//
//  RW_UserSingleViewCell.m
//  SleepApp
//
//  Created by chuangqi on 2021/3/30.
//  Copyright © 2021 RiceWang. All rights reserved.
//

#import "RW_UserSingleItemViewCell.h"
#import "RW_UserSingleRenameView.h"
#import "RW_RemoveView.h"
#import "RW_UserSingleModel.h"
@interface RW_UserSingleItemViewCell (){
    RW_UserSingleItemViewCellModel *_model;
}
/** 标题 */
@property (nonatomic,strong) UILabel *wlclass_title_label;
/** 选中icon 在添加歌单歌曲关系处有用 */
@property (nonatomic,strong) UIImageView *wlclass_select_image;



/** 删除 */
@property (nonatomic,strong) UIButton *wlclass_delete_button;
/** 重命名 */
@property (nonatomic,strong) UIButton *wlclass_rename_button;
@end
@implementation RW_UserSingleItemViewCell

-(void)bind_row_data:(WLBaseCollectionViewLayerRow *)sender{
    if ([sender.data isKindOfClass:RW_UserSingleItemViewCellModel.class]){
        _model = sender.data;
        
        self.wlclass_title_label.text = _model.title_string;
        
        if (_model.item_type == UserSingle_Item_Type_Add_UserSingle){
            self.wlclass_select_image.hidden = !_model.isSelect;
        } else if (_model.item_type == UserSingle_Item_Type_UserSingle){
            self.wlclass_delete_button.hidden = !_model.isEdit;
            self.wlclass_rename_button.hidden = !_model.isEdit;
        }
        
    }
}
-(void)bind_delegate:(id)sender{
    self.delegate = sender;
}

-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = UIColor.whiteColor;
        [self addSubview:self.wlclass_title_label];
        [self addSubview:self.wlclass_select_image];
        
        [self addSubview:self.wlclass_rename_button];
        [self addSubview:self.wlclass_delete_button];
        
        [self.wlclass_title_label mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.bottom.mas_equalTo(self);
            make.left.mas_equalTo(self).mas_offset(15);
            make.right.mas_equalTo(self).mas_offset(-50);
        }];
        [self.wlclass_select_image mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.mas_equalTo(self);
            make.right.mas_equalTo(self).mas_offset(-5);
            make.size.mas_equalTo(CGSizeMake(30, 30));
        }];
        
        [self.wlclass_delete_button mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.mas_equalTo(self);
            make.right.mas_equalTo(self).mas_offset(-15);
        }];
        [self.wlclass_rename_button mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.mas_equalTo(self);
            make.right.mas_equalTo(self.wlclass_delete_button.mas_left).mas_offset(-15);
        }];
    }
    return self;
}
#pragma mark -- 操作
-(void)click_rename_button:(UIButton *)button{
    RW_UserSingleRenameView.init_wl()
    .maskAlpha_wl(.5f)
    .addData_wl(_model)
    .closeBlock_wl(^(id data){
        self->_model.title_string  = data;
        self.wlclass_title_label.text = data;
    }).show_wl();
}

-(void)click_delete_button:(UIButton *)button{
    __WEAK_SELF_YLSLIDE
    RW_RemoveView.init_wl()
    .maskAlpha_wl(.5f)
    .addData_wl(_model)
    .closeBlock_wl(^(RW_UserSingleItemViewCellModel *data){
        [[[RW_UserSingleModel deleteToUserSingleId:data.user_single_model.xid] then:^id _Nullable(NSString * _Nullable value) {
            if ([weakSelf.delegate respondsToSelector:@selector(reloadList)]){
                [weakSelf.delegate reloadList];
            }
            return nil;
        }] catch:^(NSError * _Nonnull error) {
            [SVProgressHUD showError_wl:error.domain];
        }] ;
    }).show_wl();
}

#pragma mark -- 懒加载
-(UIButton *)wlclass_delete_button{
    if (!_wlclass_delete_button){
        _wlclass_delete_button = [[UIButton alloc]init];
        _wlclass_delete_button.hidden = true;
        [_wlclass_delete_button setImage:[UIImage svgName:@"user_song_delete"] forState:UIControlStateNormal];
        [_wlclass_delete_button addTarget:self action:@selector(click_delete_button:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _wlclass_delete_button;
}
-(UIButton *)wlclass_rename_button{
    if (!_wlclass_rename_button){
        _wlclass_rename_button = [[UIButton alloc]init];
        _wlclass_rename_button.hidden = true;
        [_wlclass_rename_button setImage:[UIImage svgName:@"user_song_rename"] forState:UIControlStateNormal];
        [_wlclass_rename_button addTarget:self action:@selector(click_rename_button:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _wlclass_rename_button;
}
-(UILabel *)wlclass_title_label{
    if (!_wlclass_title_label){
        _wlclass_title_label = [[UILabel alloc]init];
    }
    return _wlclass_title_label;
}
-(UIImageView *)wlclass_select_image{
    if (!_wlclass_select_image){
        _wlclass_select_image = [[UIImageView alloc]initWithImage:[UIImage svgName:@"select_icon"]];
        _wlclass_select_image.hidden = YES;
    }
    return _wlclass_select_image;
}
@end

@implementation RW_UserSingleItemViewCellModel
-(NSString *)title_string{
    if (!_title_string){
        _title_string = self.user_single_model.name;
    }
    return _title_string;
}
@end
